3 // Copyright (C) 2005 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32 // Permission to use, copy, modify, sell, and distribute this software
33 // is hereby granted without fee, provided that the above copyright
34 // notice appears in all copies, and that both that copyright notice and
35 // this permission notice appear in supporting documentation. None of
36 // the above authors, nor IBM Haifa Research Laboratories, make any
37 // representation about the suitability of this software for any
38 // purpose. It is provided "as is" without express or implied warranty.
41 * @file ov_tree_map_.hpp
42 * Contains an implementation class for ov_tree_.
47 #include <ext/pb_assoc/trivial_iterator_def.hpp>
48 #include <ext/pb_assoc/tree_policy.hpp>
49 #include <ext/pb_assoc/detail/eq_fn/eq_by_less.hpp>
50 #include <ext/pb_assoc/detail/types_traits.hpp>
51 #include <ext/pb_assoc/detail/map_debug_base.hpp>
52 #include <ext/pb_assoc/detail/type_utils.hpp>
53 #include <ext/pb_assoc/exception.hpp>
59 #ifdef PB_ASSOC_BASIC_REGRESSION
60 #include <pb_assoc/testsuite/regression/basic_test/throw_prob_adjustor.hpp>
61 #endif // #ifdef PB_ASSOC_BASIC_REGRESSION
69 #ifdef PB_ASSOC_OV_TREE_DEBUG_
70 #define PB_ASSOC_DBG_ASSERT(X) assert(X);
71 #define PB_ASSOC_DBG_VERIFY(X) PB_ASSOC_DBG_ASSERT(X)
72 #define PB_ASSOC_DBG_ONLY(X) X
73 #else // #ifdef PB_ASSOC_OV_TREE_DEBUG_
74 #define PB_ASSOC_DBG_ASSERT(X) ((void)0)
75 #define PB_ASSOC_DBG_VERIFY(X) X
76 #define PB_ASSOC_DBG_ONLY(X) ;
77 #endif // #ifdef PB_ASSOC_OV_TREE_DEBUG_
79 #define PB_ASSOC_CLASS_T_DEC \
87 #ifdef PB_ASSOC_DATA_TRUE_INDICATOR
88 #define PB_ASSOC_OV_TREE_CLASS_NAME \
90 #endif // #ifdef PB_ASSOC_DATA_TRUE_INDICATOR
92 #ifdef PB_ASSOC_DATA_FALSE_INDICATOR
93 #define PB_ASSOC_OV_TREE_CLASS_NAME \
95 #endif // #ifdef PB_ASSOC_DATA_FALSE_INDICATOR
97 #define PB_ASSOC_CLASS_C_DEC \
98 PB_ASSOC_OV_TREE_CLASS_NAME< \
105 #define PB_ASSOC_TYPES_TRAITS_C_DEC \
111 #ifdef PB_ASSOC_USE_MAP_DEBUG_BASE
112 #define PB_ASSOC_MAP_DEBUG_BASE_C_DEC \
113 pb_assoc::detail::map_debug_base< \
115 eq_by_less<Key, Cmp_Fn> >
116 #endif // #ifdef PB_ASSOC_USE_MAP_DEBUG_BASE
118 #ifdef PB_ASSOC_DATA_TRUE_INDICATOR
119 #define PB_ASSOC_V2F(X) (X).first
120 #define PB_ASSOC_V2S(X) (X).second
121 #define PB_ASSOC_EP2VP(X)& ((X)->m_value)
122 #endif // #ifdef PB_ASSOC_DATA_TRUE_INDICATOR
124 #ifdef PB_ASSOC_DATA_FALSE_INDICATOR
125 #define PB_ASSOC_V2F(X) (X)
126 #define PB_ASSOC_V2S(X) Mapped_Data()
127 #define PB_ASSOC_EP2VP(X)& ((X)->m_value.first)
128 #endif // #ifdef PB_ASSOC_DATA_FALSE_INDICATOR
130 template<typename Key,
135 class PB_ASSOC_OV_TREE_CLASS_NAME :
136 #ifdef PB_ASSOC_OV_TREE_DEBUG_
137 protected PB_ASSOC_MAP_DEBUG_BASE_C_DEC,
138 #endif // #ifdef PB_ASSOC_OV_TREE_DEBUG_
140 public PB_ASSOC_TYPES_TRAITS_C_DEC,
146 typedef typename Allocator::size_type size_type;
148 typedef typename Allocator::difference_type difference_type;
151 typename PB_ASSOC_TYPES_TRAITS_C_DEC::const_key_reference
154 typedef typename PB_ASSOC_TYPES_TRAITS_C_DEC::data_type data_type;
157 typename PB_ASSOC_TYPES_TRAITS_C_DEC::data_reference
161 typename PB_ASSOC_TYPES_TRAITS_C_DEC::const_data_reference
162 const_data_reference;
164 typedef typename PB_ASSOC_TYPES_TRAITS_C_DEC::value_type value_type;
166 typedef typename PB_ASSOC_TYPES_TRAITS_C_DEC::pointer pointer;
169 typename PB_ASSOC_TYPES_TRAITS_C_DEC::const_pointer
172 typedef typename PB_ASSOC_TYPES_TRAITS_C_DEC::reference reference;
175 typename PB_ASSOC_TYPES_TRAITS_C_DEC::const_reference
178 typedef const_pointer const_find_iterator;
180 typedef pointer find_iterator;
182 typedef const_find_iterator const_iterator;
184 typedef find_iterator iterator;
186 typedef pointer value_pointer;
188 #include <ext/pb_assoc/detail/ov_tree_map_/node_iterators.hpp>
190 #include <ext/pb_assoc/detail/ov_tree_map_/cond_dtor.hpp>
192 typedef Cmp_Fn cmp_fn;
194 typedef Allocator allocator;
196 typedef PB_ASSOC_TYPES_TRAITS_C_DEC my_traits_base;
198 typedef cmp_fn my_cmp_fn_base;
200 #ifdef PB_ASSOC_USE_MAP_DEBUG_BASE
201 typedef PB_ASSOC_MAP_DEBUG_BASE_C_DEC my_map_debug_base;
202 #endif // #ifdef PB_ASSOC_USE_MAP_DEBUG_BASE
206 PB_ASSOC_OV_TREE_CLASS_NAME();
208 PB_ASSOC_OV_TREE_CLASS_NAME(const Cmp_Fn& r_cmp_fn);
210 PB_ASSOC_OV_TREE_CLASS_NAME(const Cmp_Fn& r_cmp_fn, const Node_Updator& r_node_updator);
212 PB_ASSOC_OV_TREE_CLASS_NAME(const PB_ASSOC_CLASS_C_DEC& r_other);
214 ~PB_ASSOC_OV_TREE_CLASS_NAME();
217 swap(PB_ASSOC_CLASS_C_DEC& r_other);
221 copy_from_range(It first_it, It last_it);
223 template<class Node_Updator_>
225 update(node_iterator it, Node_Updator_* p_updator)
227 if (it == node_end())
230 update(it.l_child(), p_updator);
231 update(it.r_child(), p_updator);
233 p_updator->operator()(it.m_p_value,(it.l_child() == node_end())? NULL : it.l_child().m_p_value,(it.r_child() == node_end())? NULL : it.r_child().m_p_value);
237 update(node_iterator /*it*/, pb_assoc::null_node_updator* )
241 cmp_with_other(const PB_ASSOC_CLASS_C_DEC& r_other) const;
258 #ifdef PB_ASSOC_DATA_TRUE_INDICATOR
259 inline data_reference
260 subscript_imp(const_key_reference r_key)
262 PB_ASSOC_DBG_ONLY(assert_valid();)
264 find_iterator it = lower_bound(r_key);
266 if (it != find_end()&& !Cmp_Fn::operator()(
270 PB_ASSOC_DBG_ONLY(my_map_debug_base::check_key_exists(r_key));
272 PB_ASSOC_DBG_ONLY(assert_valid();)
277 PB_ASSOC_DBG_ONLY(assert_valid();)
279 return (insert_new_val(it,
280 std::make_pair(r_key, data_type()))->second);
283 inline const_data_reference
284 subscript_imp(const_key_reference r_key) const
286 PB_ASSOC_DBG_ONLY(assert_valid();)
288 PB_ASSOC_DBG_ONLY(my_map_debug_base::check_key_exists(r_key));
290 find_iterator it = lower_bound(r_key);
292 PB_ASSOC_DBG_ASSERT(it != find_end());
296 #endif // #ifdef PB_ASSOC_DATA_TRUE_INDICATOR
298 inline std::pair<find_iterator, bool>
299 insert(const_reference r_value)
301 PB_ASSOC_DBG_ONLY(assert_valid();)
303 const_key_reference r_key = PB_ASSOC_V2F(r_value);
305 find_iterator it = lower_bound(r_key);
307 if (it != find_end()&& !Cmp_Fn::operator()(
311 PB_ASSOC_DBG_ONLY(assert_valid();)
313 PB_ASSOC_DBG_ONLY(my_map_debug_base::check_key_exists(r_key));
315 return (std::make_pair(it, false));
318 PB_ASSOC_DBG_ONLY(assert_valid();)
320 return (std::make_pair(insert_new_val(it, r_value), true));
323 inline static pointer
324 mid_pointer(pointer p_begin, pointer p_end)
326 PB_ASSOC_DBG_ASSERT(p_end >= p_begin);
328 return (p_begin + (p_end - p_begin) / 2);
332 lower_bound(const_key_reference r_key)
334 pointer it = m_a_values;
336 difference_type dist = m_size;
340 const difference_type mid_dist = dist >> 1;
342 pointer mid_it = it + mid_dist;
344 if (my_cmp_fn_base::operator()(
345 PB_ASSOC_V2F(*(it + mid_dist)),
350 dist -= mid_dist + 1;
359 inline const_find_iterator
360 lower_bound(const_key_reference r_key) const
362 return (const_cast<PB_ASSOC_CLASS_C_DEC& >(*this).lower_bound(r_key));
366 upper_bound(const_key_reference r_key)
368 iterator pot_it = lower_bound(r_key);
370 if (pot_it != find_end()&& !Cmp_Fn::operator()(
372 PB_ASSOC_V2F(*pot_it)))
374 PB_ASSOC_DBG_ONLY(my_map_debug_base::check_key_exists(r_key));
379 PB_ASSOC_DBG_ONLY(my_map_debug_base::check_key_does_not_exist(r_key));
384 inline const_find_iterator
385 upper_bound(const_key_reference r_key) const
387 return (const_cast<PB_ASSOC_CLASS_C_DEC& >(*this).upper_bound(r_key));
391 find(const_key_reference r_key)
393 PB_ASSOC_DBG_ONLY(assert_valid();)
395 iterator pot_it = lower_bound(r_key);
397 if (pot_it != find_end()&& !Cmp_Fn::operator()(
399 PB_ASSOC_V2F(*pot_it)))
401 PB_ASSOC_DBG_ONLY(my_map_debug_base::check_key_exists(r_key));
406 PB_ASSOC_DBG_ONLY(my_map_debug_base::check_key_does_not_exist(r_key));
411 inline const_find_iterator
412 find(const_key_reference r_key) const
414 return (const_cast<PB_ASSOC_CLASS_C_DEC& >(*this).find(r_key));
418 erase(const_key_reference r_key);
432 join(PB_ASSOC_CLASS_C_DEC& r_other);
435 split(const_key_reference r_key, PB_ASSOC_CLASS_C_DEC& r_other);
443 inline const_iterator
455 inline const_iterator
467 inline const_iterator
473 inline const_node_iterator
476 return (const_node_iterator(mid_pointer(begin(), end()), begin(), end()));
482 return (node_iterator(mid_pointer(begin(), end()), begin(), end()));
485 inline const_node_iterator
488 return (const_node_iterator(end(), end(), end()));
494 return (node_iterator(end(), end(), end()));
500 insert_new_val(iterator it, const_reference r_value)
502 PB_ASSOC_DBG_ONLY(assert_valid();)
504 #ifdef PB_ASSOC_BASIC_REGRESSION
505 throw_prob_adjustor adjust(m_size);
506 #endif // #ifdef PB_ASSOC_BASIC_REGRESSION
508 PB_ASSOC_DBG_ONLY(my_map_debug_base::check_key_does_not_exist(
509 PB_ASSOC_V2F(r_value)));
511 pointer a_values = s_alloc.allocate(m_size + 1);
513 iterator source_it = begin();
514 iterator source_end_it = end();
515 iterator target_it = a_values;
518 cond_dtor cd(a_values, target_it, m_size + 1);
520 while (source_it != it)
522 new (const_cast<void* >(
523 static_cast<const void* >(target_it)))
524 value_type(*source_it++);
529 new (const_cast<void* >(
530 static_cast<const void* >(ret_it = target_it)))
535 while (source_it != source_end_it)
537 new (const_cast<void* >(
538 static_cast<const void* >(target_it)))
539 value_type(*source_it++);
548 cond_dtor cd1(m_a_values, m_end_it, m_size);
553 m_a_values = a_values;
555 m_end_it = m_a_values + m_size;
557 PB_ASSOC_DBG_ONLY(my_map_debug_base::insert_new(
558 PB_ASSOC_V2F(r_value)));
560 update(node_begin(), (Node_Updator* )this);
562 PB_ASSOC_DBG_ONLY(PB_ASSOC_CLASS_C_DEC::assert_valid();)
567 #ifdef PB_ASSOC_OV_TREE_DEBUG_
570 assert_valid() const;
573 assert_iterators() const;
575 #endif // #ifdef PB_ASSOC_OV_TREE_DEBUG_
579 copy_from_ordered_range(It first_it, It last_it);
583 copy_from_ordered_range(It first_it, It last_it, It other_first_it, It other_last_it);
587 typename PB_ASSOC_TYPES_TRAITS_C_DEC::value_type_allocator
592 static value_allocator s_alloc;
599 #include <ext/pb_assoc/detail/ov_tree_map_/constructors_destructor_fn_imps.hpp>
600 #include <ext/pb_assoc/detail/ov_tree_map_/iterators_fn_imps.hpp>
601 #include <ext/pb_assoc/detail/ov_tree_map_/debug_fn_imps.hpp>
602 #include <ext/pb_assoc/detail/ov_tree_map_/erase_fn_imps.hpp>
603 #include <ext/pb_assoc/detail/ov_tree_map_/insert_fn_imps.hpp>
604 #include <ext/pb_assoc/detail/ov_tree_map_/find_fn_imps.hpp>
605 #include <ext/pb_assoc/detail/ov_tree_map_/info_fn_imps.hpp>
606 #include <ext/pb_assoc/detail/ov_tree_map_/split_join_fn_imps.hpp>
608 #undef PB_ASSOC_CLASS_C_DEC
610 #undef PB_ASSOC_CLASS_T_DEC
612 #undef PB_ASSOC_OV_TREE_CLASS_NAME
614 #undef PB_ASSOC_TYPES_TRAITS_C_DEC
616 #undef PB_ASSOC_MAP_DEBUG_BASE_C_DEC
619 #undef PB_ASSOC_EP2VP
622 #undef PB_ASSOC_DBG_ASSERT
623 #undef PB_ASSOC_DBG_VERIFY
624 #undef PB_ASSOC_DBG_ONLY
626 } // namespace detail
628 } // namespace pb_assoc